package com.runvp.handler;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.constant.ConstantRedisKey;
import com.runvp.common.core.redis.RedisCache;
import com.runvp.common.core.redis.RedisLockClient;
import com.runvp.common.enums.NettyCmd;
import com.runvp.common.enums.NettyType;
import com.runvp.common.model.tcp.MessageReceiveBody;
import com.runvp.common.model.tcp.MessageResBody;
import com.runvp.common.util.ToolUtils;
import com.runvp.service.biz.domain.BizCustomer;
import com.runvp.service.biz.domain.BizCustomerWallet;
import com.runvp.service.tcp.TcpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CmdSwipeCardBalanceHandler {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private RedisLockClient lockClient;
    @Autowired
    private TcpService tcpService;
    /**
     * 设备刷卡请求余额处理
     * @param parseBody
     * @return MessageResBody
     */
    public MessageResBody handle(MessageReceiveBody parseBody){
        log.info("设备刷卡请求余额处理:::receiveBody:{}",JSONObject.toJSONString(parseBody));
        String batchNo = System.nanoTime()+"";
        String sn = parseBody.getTerminalId();
        String cardNo = parseBody.getBodyJson().getString("cid");
        long id = ToolUtils.objTOLong(tcpService.instanceRedisUtil().getCacheObject(ConstantRedisKey.DEVICE_SN_ID+sn));
        tcpService.addBizTcpRecord(parseBody.setBatchNo(batchNo));
        MessageResBody resBody = buildRes(cardNo);
        resBody.setDeviceId(id);
        resBody.setTerminalId(sn);
        tcpService.addBizTcpRecord(resBody.setBatchNo(batchNo));
        log.info("设备刷卡请求余额处理:::resBody:{}",JSONObject.toJSONString(resBody));
        return resBody;
    }
    /**
     * 结构结果体
     * @return str
     */
    private JSONObject buildBody(String cardNo){
        //根据卡号查询余额
        JSONObject map = new JSONObject();
        BizCustomer customer = tcpService.instanceIBizCustomerService().selectBizCustomerByOpenId(cardNo);
        if(null == customer){
            map.put("amt",0.0);
            log.error("卡号【{}】不存在",cardNo);
        }else {
            BizCustomerWallet wallet = tcpService.instanceIBizCustomerWalletService().customerWallet(customer.getId());
            map.put("amt",wallet.getBalance());
        }
        return map;
    }


    /**
     * 默认报文
     *
     * @return MessageResBody
     */
    public MessageResBody buildRes(String cardNo) {
        MessageResBody resBody = new MessageResBody(NettyType.SERVER_RECEIVE_DEVICE.getCode(), NettyCmd.SERVER_CARD_BALANCE.getCode());
        resBody.setDid("000000000000");
        resBody.setVersion("01");
        resBody.setBodyJson(buildBody(cardNo)).buildRes();
        log.info("服务器组装刷卡请求余额报文完毕");
        return resBody;
    }

    public static void main(String[] args) {
        System.out.println(new CmdSwipeCardBalanceHandler().buildRes("03005853").buildCmd());
    }

}
